package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.entity.Area;
import com.blb.entity.IoWarehouse;
import com.blb.entity.Product;
import com.blb.entity.Warehouse;
import com.blb.service.IIoWarehouseService;
import com.blb.service.IProductService;
import com.blb.service.IWarehouseService;
import com.blb.utils.DataTablePage;
import com.blb.utils.DataTablesPage;
import com.blb.utils.Result;
import com.blb.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 仓库管理的出入库表 前端控制器
 * </p>
 *
 * @author xiaoxiong
 * @since 2020-05-20
 */
@Controller
@RequestMapping("/io-warehouse")
public class IoWarehouseController {

    @Autowired
    private IIoWarehouseService warehouseService;

    @Autowired
    private IProductService productService;

    @Autowired
    private IWarehouseService warehouseService2;
    @RequiresPermissions("storychu")
    @ResponseBody
    @RequestMapping("/all")
    public DataTablesPage list(Integer draw,String ioNo,String batch,Integer start, Integer length) {
        Map<String,Object> map=new HashMap<>();
        map.put("start",start);
        map.put("length",length);
        map.put("ioNo",ioNo);
        map.put("batch",batch);
        int count = warehouseService.count(map);
        List<IoWarehouse> list = warehouseService.warehouseAll(map);
        return new DataTablesPage(draw,count,count,list);
    }
    @RequiresPermissions("storychu")
    @RequestMapping("/oneAll")
    public DataTablePage oneAll(String ioNo, String batch, Integer start, Integer length) {
        System.out.println("sss");
        System.out.println("ioNo:" + ioNo);
        System.out.println("batch:" + batch);
        QueryWrapper<IoWarehouse> wrapper = new QueryWrapper<>();
        wrapper.eq("io_no", ioNo);
        wrapper.eq("batch", batch);
        Page<IoWarehouse> page = new Page<>(start, length);
        page = warehouseService.page(page, wrapper);

        return new DataTablePage<IoWarehouse>(page.getTotal(), page.getTotal(), page.getRecords());
    }


    @RequiresPermissions("storyhouse")
    @ResponseBody
    @RequestMapping("/warehouseAll")
    //获取仓库
    public List<Warehouse> warehouseAll() {
        String company = ShiroUtils.getCompany();
        QueryWrapper<Warehouse> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("company_id",company);
        return warehouseService2.list(queryWrapper);
    }

    @ResponseBody
    @RequestMapping("/areaAll")
    //获取库区
    public List<Map> areaAll(String warehouseName) {
        System.out.println("warehouseName：" + warehouseName);
        List<Map> maps = warehouseService.areaName(warehouseName);
        return maps;
    }


    @ResponseBody
    @RequestMapping("/shelfAll")
    //获取货架
    public List<Map> shelfAll(String warehouseName, String areaName) {
        System.out.println("warehouseName：" + warehouseName);
        System.out.println("areaName：" + areaName);
        List<Map> maps = warehouseService.shelfNo(warehouseName, areaName);
        return maps;
    }

    @ResponseBody
    @RequestMapping("/spaceAll")
    //获取货位
    public List<Map> spaceAll(String warehouseName, String areaName, String shelfNo) {
        System.out.println("warehouseName：" + warehouseName);
        System.out.println("areaName：" + areaName);
        System.out.println("shelfNo：" + shelfNo);
        List<Map> maps = warehouseService.spaceNo(warehouseName, areaName, shelfNo);
        return maps;
    }

    @RequiresPermissions("storychu")
    @ResponseBody
    @RequestMapping("/storageAll")
    //获取入库商品信息(采购订单入库)
    public List<Map> storageAll(String type) {
        System.out.println(type);
        if(type.equals("采购订单入库")){
            List<Map> maps = warehouseService.storageAll();
            return maps;
        }else if (type.equals("生产入库")){
            List<Map> maps = warehouseService.handoverAll();
            return maps;
        }else {
            return null;
        }

    }
    @RequiresPermissions("storychu")
    @ResponseBody
    @RequestMapping("/handoverAll")
    //获取入库商品信息(生产入库)
    public List<Map> handoverAll(){
        List<Map> maps = warehouseService.handoverAll();
        return maps;
    }

    @RequiresPermissions("storychu:add")
    @ResponseBody
    @RequestMapping("/storageAdd")
    //添加入库
    public Result storageAdd(@RequestParam Map map) {
        System.out.println("入库添加的Map:" + map);
        try {
            warehouseService.storageAdd(map);
            return new Result(true, "入库成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "入库失败");
        }
    }
    @RequiresPermissions("storychu")
    @ResponseBody
    @RequestMapping("/outAll")
    //出库商品信息
    public List<Map> outAll(){
        return warehouseService.outAll();
    }

    @RequiresPermissions("storychu:add")
    @ResponseBody
    @RequestMapping("/outAdd")
    //添加出库
    public Result outAdd(@RequestParam Map map){
        System.out.println("出库添加的Map:" + map);
        try {
            warehouseService.outAdd(map);
            return new Result(true,"出库成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"出库失败");
        }
    }

}

